package com.gs.SynPatrolRep.adapter.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.control.EventItem;
import com.gs.SynPatrolRep.control.ItemText;
import com.gs.SynPatrolRep.control.WEqualsHImageView;
import com.gs.SynPatrolRep.util.DensityUtil;
import com.gs.SynPatrolRep.util.ImageUtil;

import java.math.BigDecimal;

public class BaseViewHolder extends RecyclerView.ViewHolder {

    private Context context;
    private SparseArray<View> mViews;
    private View convertView;

    public BaseViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        convertView = itemView;
        mViews = new SparseArray<>();
    }

    public static BaseViewHolder get(Context context, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return new BaseViewHolder(context, itemView);
    }

    public <T extends View> T fd(int id) {
        View view = mViews.get(id);
        if (view == null) {
            view = convertView.findViewById(id);
            mViews.put(id, view);
        }
        return (T) view;
    }

    public void setImageResource(Context activity, int id, String resource) {
        ImageView iv = fd(id);
        ImageUtil.getInstance().loadImg(activity, resource, iv);
    }

    public void setImageResource(int id, int resource) {
        ImageView iv = fd(id);
        iv.setImageResource(resource);
    }

    public void setVisibility(int id, int visible) {
        View v = fd(id);
        v.setVisibility(visible);
        v.setOnClickListener(null);
    }

    public void setHint(int id, String text) {
        TextView tx = convertView.findViewById(id);
        tx.setText("");
        tx.setHint(text);
    }

    public void setText(int id, String text) {
        TextView tv = fd(id);
        tv.setText(text);
    }

    public void setSelected(int id, boolean select) {
        View tv = fd(id);
        tv.setSelected(select);
    }

    public void setOnClickListener(int id, View.OnClickListener listener) {
        View view = convertView.findViewById(id);
        view.setVisibility(View.VISIBLE);
        view.setOnClickListener(listener);
    }

    public void setItemText(int id, String text) {
        ItemText tv = fd(id);
        tv.setText(text);
    }

    public void setEventText(int id, String text) {
        EventItem tv = fd(id);
        tv.setVisibility(View.VISIBLE);
        tv.setText(text);
    }

    public void setTextAndColor(int id, String text, int color) {
        TextView tv = fd(id);
        tv.setText(text);
        tv.setTextColor(context.getResources().getColor(color));
    }

    public void setInsightPercentage(Float num, String color) {
        TextView view = fd(R.id.tv_num);
        if (num == null) {
            view.setText("-");
        } else {
            BigDecimal b = new BigDecimal(String.valueOf(num));
            BigDecimal c = new BigDecimal(String.valueOf(100));
            view.setText(b.multiply(c).doubleValue() + "%");
        }
        GradientDrawable gd = (GradientDrawable) view.getBackground();
        try {
            view.setTextColor(Color.parseColor(color));
            gd.setColor(context.getResources().getColor(R.color.color_f2));
            gd.setStroke(DensityUtil.dp2px(4), Color.parseColor(color));
        } catch (Exception e) {
            view.setTextColor(Color.parseColor("#ff0000"));
            gd.setStroke(DensityUtil.dp2px(4), Color.parseColor("#ff0000"));
        }
    }
}
